package cn.frontop.funswitch.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author Yun
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String message = "";

    /**
     * 返回代码
     */
    private Integer code = 200;
    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 请求ID
     */
    private String requestId;

    public Result() {
    }

    /**
     * 兼容VUE3版token失效不跳转登录页面
     *
     * @param code
     * @param message
     */
    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = ResultEnum.SUCCESS.respCode;
        this.success = true;
        return this;
    }


    public static <T> Result<T> ok() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ResultEnum.SUCCESS.respCode);
        return r;
    }

    public static <T> Result<T> ok(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ResultEnum.SUCCESS.respCode);
        r.setMessage(msg);
        r.setResult((T) msg);
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ResultEnum.SUCCESS.respCode);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> ok(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(ResultEnum.SUCCESS.respCode);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> error(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(ResultEnum.ERROR.respCode);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(ResultEnum.ERROR.respCode);
        r.setMessage(msg);
        r.setResult(null);
        return r;
    }


    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public enum ResultEnum {
        //定义返回值内容
        // 0-100000 公共
        // 100000  data
        // 200000  apiManage
        // 300000  dataStorage
        // 400000  fileManage
        SUCCESS(200, "处理成功"),
        ERROR_CODE(500, "第三方返回错误信息,请检查配置!"),
        ERROR(500, "处理失败");
        private Integer respCode;
        private String respDesc;

        ResultEnum(Integer respCode, String respDesc) {
            this.respCode = respCode;
            this.respDesc = respDesc;
        }

        public Integer getRespCode() {
            return respCode;
        }

        public String getRespDesc() {
            return respDesc;
        }
    }

}
